package com.example.administrator.fishbone.db;


import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import com.example.administrator.fishbone.utils.LinkmanUtils;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/9/1.
 */
public class DishTable {
    private final String mTag = this.getClass().getName();
    private SQLiteDatabase db;

    private DishTable() {

    }

    private static class SingletonHolder {
        private static DishTable msiah = new DishTable();
    }

    /**
     * 获取当前实例
     * @return
     */
    public static DishTable dishTable() {

        return SingletonHolder.msiah;
    }

    public void init(Context context) {
        String userName = "Linkmans";
        if (!TextUtils.isEmpty(userName)) {
            LinkmanHelper helper =
                    new LinkmanHelper(context, userName, null, 1);
            db = helper.getReadableDatabase();
        }
    }
    //插入数据
    public  boolean insert(LinkmanUtils who) {
        String insertsql ="INSERT OR REPLACE INTO Linkman (_id,name)\n" +
                "\" +\n" + "\"  VALUES(?,?)" ;
        Object[] values = {who.getItemId(), who.getTVname()};
        boolean result;
        try {
            if (db != null) {
                db.execSQL(insertsql, values);
            }
            result = true;
        } catch (SQLException e) {
            result = false;
        }
        return result;

    }
    public  boolean insert(List<LinkmanUtils>mlist) {
        db.beginTransaction();
        boolean isSuccess = false;
        try {
            for (LinkmanUtils utils : mlist) {
                insert(utils);
            }
            db.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {

            db.endTransaction();
        }
        return isSuccess;
    }

//    public Cursor find() {
//        if (db != null) {
//            return null;
//
//        }
//        Cursor cursor = db.rawQuery("SELECT * FROM Linkman", null);
//        return cursor;
//    }

    public List<LinkmanUtils> getmList() {
        if (db == null) {
            return null;
        }
        Cursor cursor =null;
        List<LinkmanUtils> mList = null;
        cursor = db.rawQuery("SELECT * FROM Linkman", null);
        if (db != null) {
            if (cursor != null) {
                mList = new ArrayList<>();
                if (cursor.moveToFirst()) {
                    for (int i = 0; i<cursor.getCount();i++) {
                        String TVname = cursor.getString(cursor.getColumnIndex("name"));
                        String nameId = cursor.getString(cursor.getColumnIndex("_id"));
                        String Ltv = cursor.getString(cursor.getColumnIndex("last"));
                        LinkmanUtils utils = new LinkmanUtils(TVname,Ltv ,nameId);
                        mList.add(utils);
                        cursor.moveToNext();
                    }
                }
                cursor.close();
            }
        }
        return mList;
    }

    private void closeDB() {
        if (null != db) {
            db.close();
        }
    }

}
